package com.atguigu.spzx.manager.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.atguigu.spzx.manager.mapper.CategoryMapper;
import com.atguigu.spzx.model.entity.product.Category;
import com.atguigu.spzx.model.vo.product.CategoryExcelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CategoryReadListener extends AnalysisEventListener<CategoryExcelVo> {
    @Autowired
    CategoryMapper categoryMapper;

    private static final int BATCH_COUNT = 5;//集合的最大容量

    //使用easyexcel自己提供的工具类，创建一个指定容量的list集合
    List<Category> categoryList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);//将excel表中的所有行数据，存储在一个list集合中，将来可能产生OOM内存溢出

    @Override
    public void invoke(CategoryExcelVo categoryExcelVo, AnalysisContext analysisContext) {
        System.out.println("读取行：" + categoryExcelVo);

        Category category = new Category();
        BeanUtils.copyProperties(categoryExcelVo,category);

        categoryList.add(category);

        if(categoryList.size()==5){
            //集合已满,立即将集合中的数据插入到数据库
            categoryMapper.addCategory(categoryList);
            //为集合重新分配空间
            categoryList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        System.out.println("所有行都读取完毕");
        categoryMapper.addCategory(categoryList);
        categoryList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    }
}
